package com.itheima.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.utils.NotDeleteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper,Clazz> implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Clazz> clazzPage(ClazzQueryParam clazzQueryParam) {
        Page<Clazz> page = new Page<>(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(clazzQueryParam.getName()),Clazz::getName,clazzQueryParam.getName());
        queryWrapper.eq(clazzQueryParam.getId()!=null,Clazz::getName,clazzQueryParam.getName());
        queryWrapper.between(clazzQueryParam.getBegin()!=null&&clazzQueryParam.getEnd()!=null,Clazz::getEndDate,clazzQueryParam.getBegin(),clazzQueryParam.getEnd());
        page = page(page, queryWrapper);
        List<Clazz> clazzList = page.getRecords();
        clazzList.forEach((clazz)->{
            if(clazz!=null) {
                Integer masterId = clazz.getMasterId();
                Emp emp = empMapper.selectById(masterId);
                if(emp!=null){
                    clazz.setMasterName(emp.getName());
                }
            }
            if(clazz.getEndDate().isBefore(LocalDate.now())){
                clazz.setStatus("已结课");
            }else if(clazz.getBeginDate().isAfter(LocalDate.now())){
                clazz.setStatus("未开班");
            }else {
                clazz.setStatus("在读");
            }
        });

        PageResult<Clazz> pageResult = new PageResult<>(page.getTotal(),page.getRecords());
        return pageResult;
    }

    @Override
    public void deleteById(Integer id) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getClazzId,id);
        List<Student> studentList = studentMapper.selectList(queryWrapper);
        if(studentList.size()>0){
            throw new NotDeleteException("该班级下有学生不能删除");
        }
        clazzMapper.deleteById(id);
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        this.save(clazz);
    }

    @Override
    public Clazz getClazzById(Integer id) {
        Clazz clazz = this.getById(id);
        if(clazz!=null){
            Integer masterId = clazz.getMasterId();
            LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Emp::getId,masterId);
            Emp emp = empMapper.selectOne(queryWrapper);
            if(emp!=null){
                clazz.setMasterName(emp.getName());
            }
        }
        return clazz;
    }

}
